package com.qubuyer.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Region;
import android.graphics.Shader;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class BeautyView extends View {
    Paint paint;
    LinearGradient shader;
    RadialGradient outShader;
    Path path = new Path();

    public BeautyView(Context context) {
        this(context, null);
    }

    public BeautyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BeautyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        shader = new LinearGradient(0, 0, w, 0, Color.parseColor("#FF8A46"), Color.parseColor("#FF4E4F"), Shader.TileMode.CLAMP);
        outShader = new RadialGradient(w >> 1, -h, h * 2, new int[]{Color.parseColor("#00FFFFFF"), Color.WHITE},new float[]{0.99f,1f}, Shader.TileMode.CLAMP);
        paint.setShader(shader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setShader(shader);
        drawShader(canvas);
        paint.setShader(outShader);
        drawShader(canvas);
    }

    private void drawShader(Canvas canvas){
        path.reset();
        canvas.save();
        canvas.clipRect(0, 0, getWidth(), getHeight());
        path.addCircle(getWidth() >> 1, -getHeight(), getHeight() * 2, Path.Direction.CCW);
        canvas.clipPath(path, Region.Op.INTERSECT);
        canvas.drawRect(0,0,getWidth(),getHeight(),paint);
        canvas.restore();
    }
}
